// pages/project_detail/project_detail.js
const db = wx.cloud.database();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project_detail: {},
    publisher: {},
    isCollect: false,
    canTapCollect: true
  },
  // --------------------------------------------------

  // “收藏”事件
  collectProject() {
    let that = this;
    console.log("canTapCollect:" + this.data.canTapCollect);
    //防止频繁点击
    if (this.data.canTapCollect == false) {
      wx.showToast({
        title: '操作频繁',
        icon: "error"
      })
      return;
    }
    if (!app.personInfo._openid) {
      wx.showToast({
        title: '系统异常',
      })
      return;
    }


    // 没收藏，现在收藏
    if (that.data.isCollect == false) {
      that.setData({
        isCollect: true
      })
      db.collection('userCollect').add({
        data: {
          type: "项目",
          uuid: that.data.project_detail._id
        },
        success: res => {
          console.log("收藏");
          wx.showToast({
            title: '收藏成功',
          })
        }
      })
    } else {
      //收藏了，现在取消收藏
      that.setData({
        isCollect: false
      })
      db.collection('userCollect').where({
        _openid: app.personInfo._openid,
        uuid: that.data.project_detail._id
      }).remove({
        success: res => {
          console.log("取消收藏");
        }
      })
    }


    that.setData({
      canTapCollect: false
    })
    setTimeout(function (res) {
      that.setData({
        canTapCollect: true
      })
    }, 3000)


  },

  // “我想加入”事件
  wantJoin() {
    let that = this;
    wx.showModal({
      content: '申请加入需提交个人简历或自荐文件，是否继续？',
      success(res) {
        if (res.confirm) {
          // console.log('用户点击确定');
          let obj = {
            project_detail: that.data.project_detail,
            publisher: that.data.publisher
          }
          app.navTmpInfo = obj;
          wx.navigateTo({
            url: '/pages/wantJoinProject/wantJoinProject',
          })
        }
      }
    })
    console.log();
  },

  // --------------------------------------------------
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let that = this;
    this.setData({
      project_detail: app.navTmpInfo
    });
    console.log(app.personInfo._openid);
    console.log(that.data.project_detail._id);
    if (!app.personInfo._openid || !that.data.project_detail._id) {
      return;
    }
    await db.collection('userCollect').where({
      _openid: app.personInfo._openid,
      type: "项目",
      uuid: that.data.project_detail._id
    }).get({
      success: res => {
        if (res.data.length == 0) {
          that.setData({
            isCollect: false
          });
          console.log("用户未收藏此项目")
        } else {
          that.setData({
            isCollect: true
          })
          console.log("用户已收藏此项目")
        }
      }
    })
    // console.log(this.data.project_detail);
    // console.log(app.navTmpInfo.publisherID);


    //阻止显示错误信息
    if (!app.navTmpInfo.publisherID) {
      return
    }
    db.collection('users').where({
      _openid: app.navTmpInfo.publisherID
    }).get({
      success: res => {
        if (res.data.length == 0) {
          console.log("查不到数据")
        } else {
          that.setData({
            publisher: res.data[0]
          })
        }
      },
      fail: res => {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})